﻿using log4net;
using Microsoft.Extensions.Configuration;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using WLYT.Common;

namespace WLYT.Repository
{
    public class DbContext
    {
        private static readonly ILog loginfo = LogManager.GetLogger("loginfo");
        public static SqlSugarScope Db = new SqlSugarScope(new List<ConnectionConfig>()
        {
            new ConnectionConfig()
            {
                ConfigId = "db1",
                DbType = DbType.SqlServer,
                ConnectionString = ConfigExtensions.Configuration.GetConnectionString("znlgConnection"),
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                MoreSettings = new ConnMoreSettings
                {
                    IsWithNoLockQuery = true//开启全局查询 with nolock
                }
            },
            new ConnectionConfig()
            {
                ConfigId = "db2",
                DbType = DbType.SqlServer,
                ConnectionString = ConfigExtensions.Configuration.GetConnectionString("znlgConnection"),
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                MoreSettings = new ConnMoreSettings
                {
                    IsWithNoLockQuery = true//开启全局查询 with nolock
                }
            },
        },
        db =>
        {
            db.GetConnectionScope("db1").Aop.OnLogExecuting = (sql, pars) =>
            {
                loginfo.Info(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                //Console.WriteLine(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            };
            db.GetConnectionScope("db2").Aop.OnLogExecuting = (sql, pars) =>
            {
                loginfo.Info(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                //Console.WriteLine(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            };
        });
    }
}
